package org.jeecg.modules.task.job;


import com.xkcoding.http.util.StringUtil;
import lombok.SneakyThrows;
import lombok.extern.slf4j.Slf4j;
import org.jeecg.common.util.DateTimeUtil;
import org.jeecg.modules.api.service.FTPListAllFilesService;
import org.jeecg.modules.paper.entity.PaperLanding;
import org.jeecg.modules.paper.service.IPaperLandingService;
import org.quartz.Job;
import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.CollectionUtils;

import java.util.ArrayList;
import java.util.List;

/**
 * 示例不带参定时任务
 *
 * @Author Scott
 */
@Slf4j
public class PaperJob implements Job {

	@Autowired
	IPaperLandingService paperLandingService;
	/**
	 * 若参数变量名修改 QuartzJobController中也需对应修改
	 */
	private String parameter;

	public void setParameter(String parameter) {
		this.parameter = parameter;
	}

	@SneakyThrows
	@Override
	public void execute(JobExecutionContext jobExecutionContext) throws JobExecutionException {
		String baseUrl  = "/aritcle/TrsApp/";
		Boolean isAll = true;
		if(!StringUtil.isEmpty(parameter) ){
			String[] split = parameter.split(",");
			if(split.length>1){
				baseUrl = baseUrl+split[1]+"/";
				isAll = false;
			}else {
				baseUrl = baseUrl+	DateTimeUtil.getTodayChar8()+"/";
				isAll = false;
			}
		}

		FTPListAllFilesService ftpListAllFiles = new FTPListAllFilesService(true);
		if (ftpListAllFiles.login("106.12.160.17", 21, "root", "Rogerwu19890507!")) {
			List<PaperLanding> list = new ArrayList<>();
			ftpListAllFiles.List(baseUrl,list,isAll);
			ftpListAllFiles.disConnection();
			if(!CollectionUtils.isEmpty(list)){
				paperLandingService.saveOrUpdateBatch(list);
			}
		}

	}
}
